package com.hwz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hwz.gulimall.product.service.CategoryBrandRelationService;
import com.hwz.gulimall.product.valid.AddValid;
import com.hwz.gulimall.product.valid.UpdateValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hwz.gulimall.product.entity.BrandEntity;
import com.hwz.gulimall.product.service.BrandService;
import com.hwz.common.utils.PageUtils;
import com.hwz.common.utils.R;



/**
 * 品牌
 *
 * @author hwz
 * @email 1281561921@qq.com
 * @date 2025-05-27 23:40:51
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoList/{brandIds}")
    @Cacheable(value = "brand:infoList", key = "#brandIds")
    public R info(@PathVariable("brandIds") List<Long> brandIds){
        List<BrandEntity> list = brandService.listByIds(brandIds);
        return R.ok().put("list", list);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddValid.class) @RequestBody BrandEntity brand){
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@Validated(UpdateValid.class)  @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        categoryBrandRelationService.brandCascadingUpdate(brand.getBrandId(), brand.getName());

        return R.ok();
    }




    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
